function traceBrowserOfMultipleMaps(handles)
% traceBrowserOfMultipleMaps
%
% Editing:
% gs april 2005 -- for mapAnalysis3p0
% --------------------------------------------------

% ABANDONED


% FIGURE 
handles.data.fig.traceBrowser.hfig = figure;
clf reset;
% TODO: units relative, etc.
set(gcf, 'Color', 'w', 'DoubleBuffer', 'on', 'Units', 'normalized', ...
    'Position', [0.1    0.5    0.8    0.4], 'Toolbar', 'figure');
colormap(flipud(colormap(jet2(256))));
uicontrol(gcf, 'Style', 'text', 'String', handles.data.map.mapActive.baseName(1:(end-1)), 'FontSize', 12, ...
    'FontWeight', 'bold', 'Units', 'normalized', 'Position', [0.03   0.93    0.5    0.05], ...
    'BackgroundColor', 'w', 'HorizontalAlignment', 'left');

% TRACES ------------------------------------------------------

% make a trace array
for m = 1 : handles.data.analysis.numberOfMaps
    string = (['handles.data.map.map' num2str(m)]);
    eval(['ydataP1(:,m) = ' string '.bsArray(:,1);']);
end

% plot single trace
handles.data.fig.traceBrowser.hax1 = axes;
set(handles.data.fig.traceBrowser.hax1, 'Position', [0.05    0.15    0.4    0.75]);
xdata = 0:(size(ydataP1,1) - 1);
handles.data.fig.traceBrowser.hcurrentTrace = plot(xdata/handles.data.acq.sampleRate, ydataP1(:,1), ...
    'Color', handles.data.analysis.colorOrder(1));
hold on;
for m = 2 : handles.data.analysis.numberOfMaps
    string = (['handles.data.map.map' num2str(m)]);
    eval(['handles.data.fig.traceBrowser.hcurrentTrace' num2str(m) ...
            ' = plot(xdata/handles.data.acq.sampleRate, ydataP1(:, ' num2str(m) '));']);
    set(gco, 'Color', handles.data.analysis.colorOrder(m));
    % data.handles.hcurrentTraceP2 = plot(xdata/data.sampleRate, ydataP2, 'g-');
end

axis tight;
set(gca, 'DataAspectRatioMode', 'auto');
set(gca, 'PlotBoxAspectRatioMode', 'auto');

% stimulus line
switch handles.data.analysis.traceType
    case 'excitation profile'
        stimOn = handles.data.analysis.stimOnEP;
    case 'input map'
        stimOn = handles.data.analysis.stimOn;
    otherwise 
end
handles.data.fig.traceBrowser.hstimLine = line([stimOn stimOn], [min(get(gca, 'YLim')) max(get(gca, 'YLim'))]);
set(handles.data.fig.traceBrowser.hstimLine, 'Color', 'c');

% TODO: window lines
% switch handles.data.analysis.traceType
%     case 'excitation profile'
%         stimOn = handles.data.analysis.stimOnEP;
%     case 'input map'
%         stimOn = handles.data.analysis.stimOn;
%     otherwise 
% end
% handles.data.fig.traceBrowser.hstimLine = line([stimOn stimOn], [min(get(gca, 'YLim')) max(get(gca, 'YLim'))]);
% set(handles.data.fig.traceBrowser.hstimLine, 'Color', 'c');

% traceAdvancer SLIDER & TEXT BOX -----------------------------
if size(handles.data.map.mapActive.bsArray, 2) > 1
    if isempty(findobj(get(gcf, 'Children'), 'Tag', 'traceSlider'))
        handles.data.fig.traceBrowser.htraceSlider = uicontrol(gcf, 'Tag', 'traceSlider', 'Style', 'slider', ...
            'min', 1, 'max', size(handles.data.map.mapActive.bsArray, 2), ...
            'SliderStep', [1/ (size(handles.data.map.mapActive.bsArray, 2) - 1) ...
                1 / (size(handles.data.map.mapActive.bsArray, 2) - 1)], ...
            'Callback', 'traceAdvancer3p0', ...
            'Units', 'normalized', 'Position', [0.5   0.75    0.06    0.04]);
    else
        handles.data.fig.traceBrowser.htraceSlider = findobj(get(gcf, 'Children'), 'Tag', 'traceSlider');
    end
    set(handles.data.fig.traceBrowser.htraceSlider, 'Value', 1);
    
    drawnow;
%     pause(.1);

    if isempty(findobj(get(gcf, 'Children'), 'Tag', 'traceEdit'))
        handles.data.fig.traceBrowser.htraceEdit = uicontrol(gcf, 'Tag', 'traceEdit', 'Style', 'edit', ...
            'min', 1, 'max', 1, 'BackgroundColor', 'w', 'FontSize', 12, ...
            'Callback', 'traceAdvancer3p0', ...
            'Units', 'normalized', 'Position', [0.5   0.79    0.06    0.04]);
    else
        handles.data.fig.traceBrowser.htraceEdit = findobj(get(gcf, 'Children'), 'Tag', 'traceEdit');
    end
    set(handles.data.fig.traceBrowser.htraceEdit, 'String', num2str(1));
end
handles.data.fig.traceBrowser.currentTrace = get(handles.data.fig.traceBrowser.htraceSlider, 'Value');
%
uicontrol(gcf, 'Style', 'text', 'String', 'current trace', 'FontSize', 10, ...
    'Units', 'normalized', 'Position', [0.5   0.83    0.06    0.04]);

% MAPS --------------------------------------------------------

% some pre-processing
[sizeX sizeY z] = size(handles.data.map.mapActive.mapPattern);
handles.data.map.mapActive.magFacX = (handles.data.map.mapActive.mapSpacing * (sizeX-1))/2;
handles.data.map.mapActive.magFacY = (handles.data.map.mapActive.mapSpacing * (sizeY-1))/2;

% mapP1
handles.data.fig.traceBrowser.haxMapP1 = axes;
set(handles.data.fig.traceBrowser.haxMapP1, 'Position', [0.5    0.15    0.1    0.35], 'box', 'on');
handles.data.fig.traceBrowser.himgP1 = imagesc(handles.data.map.mapActive.mapPattern);
% handles.data.fig.traceBrowser.himgP1 = imagesc(handles.data.map.mapActive.mapDataArray(:,:,9));
set(handles.data.fig.traceBrowser.himgP1, 'XData', [-handles.data.map.mapActive.magFacX handles.data.map.mapActive.magFacX], ...
    'YData', [-handles.data.map.mapActive.magFacY handles.data.map.mapActive.magFacY]);
set(gca, 'PlotBoxAspectRatio', [1 1 1]);
axis tight;

% % mapP2
% data.handles.haxMapP2 = axes;
% set(data.handles.haxMapP2, 'Position', [0.6809    0.4169    0.2061    0.1883], 'box', 'on');
% data.handles.himgP2 = imagesc(data.map.mapPattern);
% set(data.handles.himgP2, 'XData', [-data.map.magFacX data.map.magFacX], ...
%     'YData', [-data.map.magFacY data.map.magFacY]);
% set(gca, 'PlotBoxAspectRatio', [1 1 1]);
% axis tight;

% mark current pixel
% P1:
axes(handles.data.fig.traceBrowser.haxMapP1);
hold on;
[row col] = find(handles.data.map.mapActive.mapPattern == handles.data.fig.traceBrowser.currentTrace);
y = (row - 1) * handles.data.map.mapActive.mapSpacing - handles.data.map.mapActive.magFacY;
x = (col - 1) * handles.data.map.mapActive.mapSpacing - handles.data.map.mapActive.magFacX;
handles.data.fig.traceBrowser.haxMap1_currentTrace = plot(x, y, 'w*');
set(handles.data.fig.traceBrowser.haxMap1_currentTrace, 'LineWidth', 1, 'MarkerSize', 12);
% % P2:
% axes(data.handles.haxMapP2);
% hold on;
% data.handles.haxMap2_currentTrace = plot(x, y, 'w*');
% set(data.handles.haxMap2_currentTrace, 'LineWidth', 1.5);
% set(data.handles.haxMap2_currentTrace, 'LineWidth', 1, 'MarkerSize', 12);

% enable pixTrace
pixTrace(handles.data.fig.traceBrowser.himgP1);
% pixTrace(data.handles.himgP2);

% % TODO: option to show mapPattern or genMean map (or other)
% cmenu = uicontextmenu('Parent', handles.data.fig.traceBrowser.haxMapP1);
% cb1 = @selectMapType(1);
% cb2 = @selectMapType(1);
% cb3 = @selectMapType(1);
% cb4 = @selectMapType(1);
% item1 = uimenu(cmenu, 'Label', 'map pattern', 'Callback', cb1);
% item2 = uimenu(cmenu, 'Label', 'synaptic responses', 'Callback', cb2);
% item3 = uimenu(cmenu, 'Label', 'direct responses', 'Callback', cb3);
% item4 = uimenu(cmenu, 'Label', 'excitation profile', 'Callback', cb4);



% SLICE IMAGE -------------------------------------------------

handles.data.fig.traceBrowser.haxSliceImg = axes;

% blank slice image
handles.data.fig.traceBrowser.hSliceImg = imagesc(magic(4));
set(handles.data.fig.traceBrowser.hSliceImg, 'Visible', 'off');
hold on;

% blank map array
handles.data.fig.traceBrowser.hBlankMap = imagesc(zeros(16,16));
set(handles.data.fig.traceBrowser.hBlankMap, 'AlphaData', .15, ...
    'XData', [-handles.data.map.mapActive.magFacX handles.data.map.mapActive.magFacX], ...
    'YData', [-handles.data.map.mapActive.magFacY handles.data.map.mapActive.magFacY]);
pixTrace(handles.data.fig.traceBrowser.hBlankMap);

% mark current pixel
[row col] = find(handles.data.map.mapActive.mapPattern == handles.data.fig.traceBrowser.currentTrace);
y = (row - 1) * handles.data.map.mapActive.mapSpacing - handles.data.map.mapActive.magFacY;
x = (col - 1) * handles.data.map.mapActive.mapSpacing - handles.data.map.mapActive.magFacX;
hold on;
handles.data.fig.traceBrowser.haxSliceImg_currentTrace = plot(x, y, 'c*');
set(handles.data.fig.traceBrowser.haxSliceImg_currentTrace, 'LineWidth', 1, 'MarkerSize', 12);

% rectangle to mark mapping region
handles.data.fig.traceBrowser.hRectMapArea = rectangle;
xdata = get(handles.data.fig.traceBrowser.himgP1, 'XData');
ydata = get(handles.data.fig.traceBrowser.himgP1, 'YData');
set(handles.data.fig.traceBrowser.hRectMapArea, 'Position', ...
    [xdata(1) ydata(1) xdata(2)-xdata(1) ydata(2)-ydata(1)], ...
    'EdgeColor', 'y', 'LineStyle', ':');

% fix up axes
set(handles.data.fig.traceBrowser.haxSliceImg, 'Position', [0.65    0.15    0.3    0.75], ...
    'ButtonDownFcn', 'addSliceImage3p0');
title('Click on axes to upload an image');
handleList = [handles.data.fig.traceBrowser.haxSliceImg_currentTrace; ...
    handles.data.fig.traceBrowser.hRectMapArea; handles.data.fig.traceBrowser.hBlankMap; ...
    handles.data.fig.traceBrowser.hSliceImg];
set(handles.data.fig.traceBrowser.haxSliceImg, 'Children', handleList); 
daspect([1 1 1]);
axis tight;

% now put in a real slice image, if previously specified
try 
    %     % read in the image file & convert to RGB format
    % %     fullname = [handles.data.image.imgDir '\' handles.data.image.imgDir
    %     I = imread(fullname);
    %     R = ind2rgb(I, gray(256));
    R = handles.data.image.img;
    
    % delete the previous stuff
    delete(handles.data.fig.traceBrowser.hSliceImg);
    
    % display image
    handles.data.fig.traceBrowser.hSliceImg = imagesc(R);
    daspect([1 1 1]);
    % set(himg, 'ButtonDownFcn', 'addSliceImage');
    set(gca, 'ButtonDownFcn', 'addSliceImage3p0');
    title('Click on axes to upload an image');
    
    % distance scaling
    try
        xrange = handles.data.acq.imageXrange;
        yrange = handles.data.acq.imageYrange;
    catch
        xrange = 1900; % default param's from Ingrid's rig
        yrange = 1520;
        disp('traceBrowser: problem with image scaling; using defaults.');
    end
    set(handles.data.fig.traceBrowser.hSliceImg, ...
        'XData', [-xrange/2 xrange/2], 'YData', [-yrange/2 yrange/2]);
    axis tight;
    
    handleList = [handles.data.fig.traceBrowser.haxSliceImg_currentTrace; ...
            handles.data.fig.traceBrowser.hRectMapArea; handles.data.fig.traceBrowser.hBlankMap; ...
            handles.data.fig.traceBrowser.hSliceImg];
    set(handles.data.fig.traceBrowser.haxSliceImg, 'Children', handleList); 
catch
    disp('traceBrowser: problem loading previously selected slice image.')
end



% % XCORR -------------------------------------------------------
% 
% % plot the basic xcorr -- 0 to 100 ms post-stim
% data.handles.hax2 = axes;
% set(data.handles.hax2, 'Position', [0.0555    0.0770    0.1838    0.2311], 'box', 'on');
% data.handles.hxcorr1 = plot(data.xcorr.XCxdata', data.xcorr.XC(:,1), 'r-');
% set(data.handles.hxcorr1, 'Tag', 'xcorr1');
% axis tight;
% set(gca, 'DataAspectRatioMode', 'auto');
% set(gca, 'PlotBoxAspectRatioMode', 'auto');
% title('xcorr');
% 
% % lines for the xcorr plot
% data.handles.hzeroLine = line([0 0], get(gca, 'YLim'));
% set(data.handles.hzeroLine, 'Color', 'k');
% 
% % indicate peaks and lags
% hold on;
% data.handles.hmaxXCorr = line([data.xcorr.maxXCorrLags(1) data.xcorr.maxXCorrLags(1)], ...
%     [get(gca, 'YLim')]);
% set(data.handles.hmaxXCorr, 'Color', 'm');
% 




% STORE DATA -------------------------------------------------
% stuff the relevant data into the UserData
set(gcf, 'UserData', handles, 'Tag', 'currentFig');
